package com.bdc.whatwhat.views.drawer;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v4.view.GravityCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bdc.whatwhat.R;
import com.bdc.whatwhat.activities.MainActivity;
import com.bdc.whatwhat.authentification.Facebook;
import com.bdc.whatwhat.fragments.APropos.AProposFragment;
import com.bdc.whatwhat.fragments.FluxFragment;
import com.bdc.whatwhat.fragments.MesWhattersFragment;
import com.bdc.whatwhat.fragments.StatsFragment;
import com.bdc.whatwhat.fragments.core.WWFragment;
import com.bdc.whatwhat.modele.Whatter;
import com.bdc.whatwhat.session.SessionManager;
import com.bdc.whatwhat.utils.transform.CircleTransform;
import com.bdc.whatwhat.utils.transform.CropTransformation;
import com.squareup.picasso.Picasso;

/**
 * Created by gfimac on 22/07/2014.
 */
public class LeftDrawer extends RelativeLayout implements View.OnClickListener{

    WWFragment activeFragment;
    FragmentActiveDelegate fragmentActiveDelegate;
    SessionManager session;
    TextView prenom, nom, status;
    ImageView imageProfil, image_drawer_left_banner;
    View mon_flux, stats, mes_amis, a_propos;


    public LeftDrawer(Context context) {
        super(context);
    }

    public LeftDrawer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public LeftDrawer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        charger();
        remplir();
        ajouterListeners();
    }


    private void charger() {
        session = new SessionManager(getContext());
        prenom = (TextView) findViewById(R.id.prenom);
        nom = (TextView) findViewById(R.id.nom);
        status = (TextView) findViewById(R.id.status);
        imageProfil = (ImageView) findViewById(R.id.imageProfil);
        image_drawer_left_banner = (ImageView) findViewById(R.id.image_drawer_left_banner);

        mon_flux = findViewById(R.id.mon_flux);
        stats = findViewById(R.id.drawer_statistiques);
        mes_amis = findViewById(R.id.mes_amis);
        a_propos = findViewById(R.id.a_propos);
    }

    private void remplir() {
        if(!isInEditMode()) {
            chargerProfilDrawerGauche();
            itemMenuSelectionner(mon_flux, R.drawable.ic_flux_white);
        }else{
            image_drawer_left_banner.setImageDrawable(getResources().getDrawable(R.drawable.streetslight));
            imageProfil.setImageDrawable(getResources().getDrawable(R.drawable.ic_card_nbr_ww_dark));
            prenom.setText("Prenom");
            nom.setText("Nom");
        }
    }

    private void ajouterListeners() {
        mon_flux.setOnClickListener(this);
        stats.setOnClickListener(this);
        mes_amis.setOnClickListener(this);
        a_propos.setOnClickListener(this);
    }

    public void chargerProfilDrawerGauche() {

        Whatter profil = session.getProfil();

        if (profil != null) {

            if (profil.getPrenom() != null && profil.getNom() != null) {
                prenom.setText(profil.getPrenom());
                nom.setText(profil.getNom());
            }

            if (profil.getWhatWhat() != null && profil.getWhatWhat().getTexte() != null) {
                String output = profil.getWhatWhat().getTexte().substring(0, 1).toUpperCase() + profil.getWhatWhat().getTexte().substring(1);
                status.setText("\"" + output + " \"");
            }

            if (profil.getUrlImage() != null) {
                Picasso.with(getContext()).load(profil.getUrlImage()).transform(new CropTransformation(300)).transform(new CircleTransform()).into(imageProfil);
            }
            if (profil.getUrlBanner() != null) {
                Picasso.with(getContext()).load(profil.getUrlBanner()).transform(new CropTransformation(500)).into(image_drawer_left_banner);
            }
        }

    }

    private void itemMenuSelectionner(View view, int ressourceID) {
        changeStyleView(view, "Roboto-Medium.ttf", R.color.txt_black, R.color.white, ressourceID);
    }

    public void resetDrawer() {
        resetStyleView(mon_flux, "Roboto-Light.ttf", R.color.selector_black_to_white, R.drawable.selector_white_to_pink, R.drawable.selector_flux);
        resetStyleView(stats, "Roboto-Light.ttf", R.color.selector_black_to_white, R.drawable.selector_white_to_pink, R.drawable.selector_stats);
        resetStyleView(mes_amis, "Roboto-Light.ttf", R.color.selector_black_to_white,R.drawable.selector_white_to_pink, R.drawable.selector_wwww);
        resetStyleView(a_propos, "Roboto-Regular.ttf", R.color.selector_black_to_white, R.drawable.selector_gray_to_pink, R.drawable.ic_drawer_footer_about);
    }

    private void changeStyleView(View view, String typeFace, int textColorId, int backgroundColor, int drawableId) {

        try {
            Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + typeFace);
            ((TextView) view.findViewById(R.id.texte)).setTypeface(typeface);
            ((TextView) view.findViewById(R.id.texte)).setTextColor(getResources().getColor(textColorId));
            ((ImageView) view.findViewById(R.id.logo)).setImageResource(drawableId);
            view.setBackgroundColor(getResources().getColor(backgroundColor));
        } catch (Exception e) {
        }

    }


    private void resetStyleView(View view, String typeFace, int textColorId, int backgroundColor, int drawableId) {
        XmlResourceParser xrp = getResources().getXml(textColorId);
        try {

            Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + typeFace);

            ((TextView) view.findViewById(R.id.texte)).setTypeface(typeface);

            ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
            ((TextView) view.findViewById(R.id.texte)).setTextColor(csl);

            ((ImageView) view.findViewById(R.id.logo)).setImageDrawable(getResources().getDrawable(drawableId));
            view.setBackgroundResource(backgroundColor);

        } catch (Exception e) {
        }

    }

    @Override
    public void onClick(final View view) {
        fragmentActiveDelegate.changeStatusDrawer(GravityCompat.START);

        postDelayed(new Runnable() {
            @Override
            public void run() {


                resetDrawer();
                int drawableId = -1;

                switch (view.getId()) {

                    case R.id.mon_flux:

                        if (activeFragment.getClass() != FluxFragment.class) {
                            activeFragment = FluxFragment.newInstance();
                            drawableId = R.drawable.ic_flux_white;
                        }

                        break;

                    case R.id.drawer_statistiques:
                        if (activeFragment.getClass() != StatsFragment.class) {
                            activeFragment = StatsFragment.newInstance();
                            drawableId = R.drawable.ic_stats_white;
                        }
                        break;

                    case R.id.mes_amis:
                        if (activeFragment.getClass() != MesWhattersFragment.class) {
                            activeFragment = MesWhattersFragment.newInstance();
                            drawableId = R.drawable.ic_earth_white;
                            Facebook facebook = new Facebook((MainActivity)getContext(),(MainActivity)getContext());
                            facebook.onResume();
                            //TODO Juste connecter la personne à facebook
                            facebook.inviteFriends();
                        }
                        break;

                }


                itemMenuSelectionner(view, drawableId);
                fragmentActiveDelegate.setFragmentActive(activeFragment);
            }
        }, 500);
    }

    public void setFragmentActiveDelegate(FragmentActiveDelegate delegate) {
        this.fragmentActiveDelegate = delegate;
    }

    public void setActiveFragment(WWFragment fragment) {
        activeFragment = fragment;
        fragmentActiveDelegate.setFragmentActive(fragment);
    }

    public void mettreAJour() {
        session = new SessionManager(getContext());
        chargerProfilDrawerGauche();
    }

    public interface FragmentActiveDelegate {
        public void setFragmentActive(WWFragment fragmentActive);

        public void changeStatusDrawer(int gravity);
    }

}
